/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.feiyue.basic.server.cache.CacheFetch;
import com.feiyue.basic.server.cache.DiskLruCache;
import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Book;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;
import java.io.File;
import java.util.List;

public class BookRankActivity extends Activity {
	public static final String boyrank = "boyrank";
	public static final String girlrank = "girlrank";
	public static final String monthrank = "monthrank";
	public static final String totalrank = "totalrank";
	public static final String weekrank = "weekrank";
	private LinearLayout area_boy;
	private LinearLayout area_girl;
	private LinearLayout area_total;
	private List<Book> booklist_boyrank;
	private List<Book> booklist_girlrank;
	private List<Book> booklist_totalrank;
	private LinearLayout boy_item1;
	private TextView boy_item1_author;
	private TextView boy_item1_introduce;
	private TextView boy_item1_title;
	private LinearLayout boy_item2;
	private TextView boy_item2_introduce;
	private TextView boy_item2_title;
	private LinearLayout boy_item3;
	private TextView boy_item3_introduce;
	private TextView boy_item3_title;
	private LinearLayout boy_item4;
	private TextView boy_item4_introduce;
	private TextView boy_item4_title;
	private LinearLayout boy_item5;
	private TextView boy_item5_introduce;
	private TextView boy_item5_title;
	private LinearLayout boy_item6;
	private TextView boy_item6_introduce;
	private TextView boy_item6_title;
	private Context context;
	private LinearLayout girl_item1;
	private TextView girl_item1_author;
	private TextView girl_item1_introduce;
	private TextView girl_item1_title;
	private LinearLayout girl_item2;
	private TextView girl_item2_introduce;
	private TextView girl_item2_title;
	private LinearLayout girl_item3;
	private TextView girl_item3_introduce;
	private TextView girl_item3_title;
	private LinearLayout girl_item4;
	private TextView girl_item4_introduce;
	private TextView girl_item4_title;
	private LinearLayout girl_item5;
	private TextView girl_item5_introduce;
	private TextView girl_item5_title;
	private LinearLayout girl_item6;
	private TextView girl_item6_introduce;
	private TextView girl_item6_title;
	private Handler handler;
	private LinearLayout loading;
	private Myrunnable myrunnable_boy;
	private Myrunnable myrunnable_girl;
	private Myrunnable myrunnable_total;
	private LinearLayout total_item1;
	private TextView total_item1_author;
	private TextView total_item1_introduce;
	private TextView total_item1_title;
	private LinearLayout total_item2;
	private TextView total_item2_introduce;
	private TextView total_item2_title;
	private LinearLayout total_item3;
	private TextView total_item3_introduce;
	private TextView total_item3_title;
	private LinearLayout total_item4;
	private TextView total_item4_introduce;
	private TextView total_item4_title;
	private LinearLayout total_item5;
	private TextView total_item5_introduce;
	private TextView total_item5_title;
	private LinearLayout total_item6;
	private TextView total_item6_introduce;
	private TextView total_item6_title;

	private void fillImage(ImageView paramImageView, String paramString) {
		int i = BitmapFactory.decodeResource(getResources(), 2130837508)
				.getWidth();
		int j = BitmapFactory.decodeResource(getResources(), 2130837508)
				.getHeight();
		Myrunnable2image localMyrunnable2image = new Myrunnable2image(
				paramImageView);
		Handler localHandler = new Handler();
		new MyThread2image(getApplicationContext(), localMyrunnable2image,
				localHandler, paramString, i, j).start();
	}

	private android.view.View.OnClickListener getitemonclick(final String flag, final int index)
    {
        return new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                List list = null;
                if (flag.equals("totalrank"))
                {
                    list = booklist_totalrank;
                } else
                {
                    if (!flag.equals("boyrank")) {
                    	if(flag.equals("girlrank")) {
                        list = booklist_girlrank;
                    	}
                    } else {
                    	list = booklist_boyrank;
                    }
                }
                
                if (list != null && list.size() > index) {
                	String s = ((Book)list.get(index)).getName();
                    int i = ((Book)list.get(index)).getId();
                    int j = ((Book)list.get(index)).getCoverid();
                    String s1 = ((Book)list.get(index)).getCoverAddr();
                    int k = ((Book)list.get(index)).getdlstate();
                    String s2 = ((Book)list.get(index)).getAuthor();
                    String s3 = ((Book)list.get(index)).getIntroduction();
                    String s4 = ((Book)list.get(index)).getIssuestate();
                    Intent intent = new Intent(getApplicationContext(), BookBriefActivity.class);
                    intent.putExtra("categoryname", flag);
                    intent.putExtra("bookname", s);
                    intent.putExtra("bookid", i);
                    intent.putExtra("author", s2);
                    intent.putExtra("introduction", s3);
                    intent.putExtra("issuestate", s4);
                    intent.putExtra("coverid", j);
                    intent.putExtra("coverAddr", s1);
                    intent.putExtra("dlstate", k);
                    startActivity(intent);
                }
                
            }
        };
    }

	private android.view.View.OnClickListener getrankonclick(final String flag)
    {
        return new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                Intent intent = new Intent(getApplicationContext(), BookRankListActivity.class);
                intent.putExtra("flag", flag);
                startActivity(intent);
            }
        };
    }

	private void initiate() {
		this.area_total = ((LinearLayout) findViewById(2131099705));
		this.total_item1_title = ((TextView) findViewById(2131099708));
		this.total_item1_author = ((TextView) findViewById(2131099709));
		this.total_item1_introduce = ((TextView) findViewById(2131099710));
		this.total_item2_title = ((TextView) findViewById(2131099712));
		this.total_item2_introduce = ((TextView) findViewById(2131099713));
		this.total_item3_title = ((TextView) findViewById(2131099715));
		this.total_item3_introduce = ((TextView) findViewById(2131099716));
		this.total_item4_title = ((TextView) findViewById(2131099718));
		this.total_item4_introduce = ((TextView) findViewById(2131099719));
		this.total_item5_title = ((TextView) findViewById(2131099721));
		this.total_item5_introduce = ((TextView) findViewById(2131099722));
		this.total_item6_title = ((TextView) findViewById(2131099724));
		this.total_item6_introduce = ((TextView) findViewById(2131099725));
		this.total_item1 = ((LinearLayout) findViewById(2131099706));
		this.total_item2 = ((LinearLayout) findViewById(2131099711));
		this.total_item3 = ((LinearLayout) findViewById(2131099714));
		this.total_item4 = ((LinearLayout) findViewById(2131099717));
		this.total_item5 = ((LinearLayout) findViewById(2131099720));
		this.total_item6 = ((LinearLayout) findViewById(2131099723));
		this.area_boy = ((LinearLayout) findViewById(2131099726));
		this.boy_item1_title = ((TextView) findViewById(2131099732));
		this.boy_item1_author = ((TextView) findViewById(2131099733));
		this.boy_item1_introduce = ((TextView) findViewById(2131099734));
		this.boy_item2_title = ((TextView) findViewById(2131099736));
		this.boy_item2_introduce = ((TextView) findViewById(2131099737));
		this.boy_item3_title = ((TextView) findViewById(2131099739));
		this.boy_item3_introduce = ((TextView) findViewById(2131099740));
		this.boy_item4_title = ((TextView) findViewById(2131099742));
		this.boy_item4_introduce = ((TextView) findViewById(2131099743));
		this.boy_item5_title = ((TextView) findViewById(2131099745));
		this.boy_item5_introduce = ((TextView) findViewById(2131099746));
		this.boy_item6_title = ((TextView) findViewById(2131099748));
		this.boy_item6_introduce = ((TextView) findViewById(2131099749));
		this.boy_item1 = ((LinearLayout) findViewById(2131099730));
		this.boy_item2 = ((LinearLayout) findViewById(2131099735));
		this.boy_item3 = ((LinearLayout) findViewById(2131099738));
		this.boy_item4 = ((LinearLayout) findViewById(2131099741));
		this.boy_item5 = ((LinearLayout) findViewById(2131099744));
		this.boy_item6 = ((LinearLayout) findViewById(2131099747));
		this.area_girl = ((LinearLayout) findViewById(2131099750));
		this.girl_item1_title = ((TextView) findViewById(2131099755));
		this.girl_item1_author = ((TextView) findViewById(2131099756));
		this.girl_item1_introduce = ((TextView) findViewById(2131099757));
		this.girl_item2_title = ((TextView) findViewById(2131099759));
		this.girl_item2_introduce = ((TextView) findViewById(2131099760));
		this.girl_item3_title = ((TextView) findViewById(2131099762));
		this.girl_item3_introduce = ((TextView) findViewById(2131099763));
		this.girl_item4_title = ((TextView) findViewById(2131099765));
		this.girl_item4_introduce = ((TextView) findViewById(2131099766));
		this.girl_item5_title = ((TextView) findViewById(2131099768));
		this.girl_item5_introduce = ((TextView) findViewById(2131099769));
		this.girl_item6_title = ((TextView) findViewById(2131099771));
		this.girl_item6_introduce = ((TextView) findViewById(2131099772));
		this.girl_item1 = ((LinearLayout) findViewById(2131099753));
		this.girl_item2 = ((LinearLayout) findViewById(2131099758));
		this.girl_item3 = ((LinearLayout) findViewById(2131099761));
		this.girl_item4 = ((LinearLayout) findViewById(2131099764));
		this.girl_item5 = ((LinearLayout) findViewById(2131099767));
		this.girl_item6 = ((LinearLayout) findViewById(2131099770));
		this.loading = ((LinearLayout) findViewById(2131099690));
		this.area_total.setVisibility(8);
		this.area_boy.setVisibility(8);
		this.area_girl.setVisibility(8);
		this.loading.setVisibility(0);
	}

	private void setonclick() {
		((TextView) findViewById(2131099702))
				.setOnClickListener(getrankonclick("weekrank"));
		((TextView) findViewById(2131099703))
				.setOnClickListener(getrankonclick("monthrank"));
		((TextView) findViewById(2131099704))
				.setOnClickListener(getrankonclick("totalrank"));
		((TextView) findViewById(2131099729))
				.setOnClickListener(getrankonclick("boyrank"));
		((TextView) findViewById(2131099752))
				.setOnClickListener(getrankonclick("girlrank"));
		this.total_item1.setOnClickListener(getitemonclick("totalrank", 0));
		this.total_item2.setOnClickListener(getitemonclick("totalrank", 1));
		this.total_item3.setOnClickListener(getitemonclick("totalrank", 2));
		this.total_item4.setOnClickListener(getitemonclick("totalrank", 3));
		this.total_item5.setOnClickListener(getitemonclick("totalrank", 4));
		this.total_item6.setOnClickListener(getitemonclick("totalrank", 5));
		this.boy_item1.setOnClickListener(getitemonclick("boyrank", 0));
		this.boy_item2.setOnClickListener(getitemonclick("boyrank", 1));
		this.boy_item3.setOnClickListener(getitemonclick("boyrank", 2));
		this.boy_item4.setOnClickListener(getitemonclick("boyrank", 3));
		this.boy_item5.setOnClickListener(getitemonclick("boyrank", 4));
		this.boy_item6.setOnClickListener(getitemonclick("boyrank", 5));
		this.girl_item1.setOnClickListener(getitemonclick("girlrank", 0));
		this.girl_item2.setOnClickListener(getitemonclick("girlrank", 1));
		this.girl_item3.setOnClickListener(getitemonclick("girlrank", 2));
		this.girl_item4.setOnClickListener(getitemonclick("girlrank", 3));
		this.girl_item5.setOnClickListener(getitemonclick("girlrank", 4));
		this.girl_item6.setOnClickListener(getitemonclick("girlrank", 5));
	}

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.bookrank);
		this.context = getApplicationContext();
		initiate();
		setonclick();
		this.myrunnable_total = new Myrunnable();
		this.myrunnable_boy = new Myrunnable();
		this.myrunnable_girl = new Myrunnable();
		this.handler = new Handler();
		new MyThread(NovelInfo.getUrl2hotBooklist(0, 6), "totalrank").start();
		new MyThread(NovelInfo.getUrl2boyhotBooklist(0, 6), "boyrank").start();
		new MyThread(NovelInfo.getUrl2girlhotBooklist(0, 6), "girlrank")
				.start();
		UmengUpdateAgent.update(getParent());
		new FeedbackAgent(this).sync();
	}

	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void setImage(List list, int i)
    {
        double d;
        double d1;
        Book book;
        ImageView imageview;
        DiskLruCache disklrucache;
        d = BitmapFactory.decodeResource(context.getResources(), 0x7f020004).getWidth();
        d1 = BitmapFactory.decodeResource(context.getResources(), 0x7f020004).getHeight();
        book = (Book)list.get(0);
        imageview = (ImageView)findViewById(i);
        if (book.getCoverAddr() == null || book.getCoverAddr().equals("null") || book.getCoverAddr().equals("")) {
        	imageview.setImageResource(book.getCoverid(Boolean.valueOf(false)));
        } else {
        	File file = DiskLruCache.getDiskCacheDir(context, "http");
            disklrucache = DiskLruCache.openCache(context, file, 0xa00000L);
            if (disklrucache == null) {
            	imageview.setImageResource(book.getCoverid(Boolean.valueOf(false)));
            } else {
            	File file1 = new File(disklrucache.createFilePath(book.getCoverAddr()));
                if (!disklrucache.containsKey(book.getCoverAddr())) {
                	imageview.setImageResource(book.getCoverid(Boolean.valueOf(false)));
                    fillImage(imageview, book.getCoverAddr());
                } else {
                	try {
                		Bitmap bitmap = BitmapFactory.decodeFile(file1.getPath());
                        
                        if (bitmap != null)
                        {
                            int j = bitmap.getWidth();
                            int k = bitmap.getHeight();
                            float f = (float)(d / (double)j);
                            float f1 = (float)(d1 / (double)k);
                            Matrix matrix = new Matrix();
                            matrix.postScale(f, f1);
                            imageview.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, j, k, matrix, true));
                        }
                	} catch (OutOfMemoryError e) {
                		System.gc();
                	}
                }
            }
        }
    }

	private class MyThread extends Thread
    {

        private List booklist;
        private String flag;
        private String url;

        public void run()
        {
            booklist = NovelInfo.getNewestBookList(context, url);
            if (booklist == null || booklist.size() == 0) {
            } else {
            	if (!flag.equals("totalrank")) {
            		if (flag.equals("boyrank"))
                    {
                        booklist_boyrank = booklist;
                        myrunnable_boy.setFlag(flag);
                        handler.post(myrunnable_boy);
                    } else
                    if (flag.equals("girlrank"))
                    {
                        booklist_girlrank = booklist;
                        myrunnable_girl.setFlag(flag);
                        handler.post(myrunnable_girl);
                    }
            	} else {
            		booklist_totalrank = booklist;
                    myrunnable_total.setFlag(flag);
                    handler.post(myrunnable_total);
            	}
            }
            
            super.run();
        }

        public MyThread(String s, String s1)
        {
            super();
            url = s;
            flag = s1;
        }
    }

	private class MyThread2image extends Thread
    {

        private Context context;
        private String coverAddress;
        private Handler handler;
        private int height;
        private Myrunnable2image myrunnable;
        private int width;

        public void run()
        {
            File file = CacheFetch.getFile(context, coverAddress, coverAddress, false);
            if (file != null) {
            	
            	try {
                    Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
                    
                    if (bitmap != null)
                    {
                        int i = bitmap.getWidth();
                        int j = bitmap.getHeight();
                        float f = (float)((double)width / (double)i);
                        float f1 = (float)((double)height / (double)j);
                        Matrix matrix = new Matrix();
                        matrix.postScale(f, f1);
                        Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true);
                        myrunnable.setBitmap(bitmap1);
                        handler.post(myrunnable);
                    }
            	} catch (OutOfMemoryError e) {
            		System.gc();
            	}
            	
            }
        }

        public MyThread2image(Context context1, Myrunnable2image myrunnable2image, Handler handler1, String s, int i, int j)
        {
            super();
            context = context1;
            myrunnable = myrunnable2image;
            handler = handler1;
            coverAddress = s;
            width = i;
            height = j;
        }
    }

    private class Myrunnable
        implements Runnable
    {
    	private String flag;

        public void run()
        {
            if (!flag.equals("totalrank") || booklist_totalrank.size() < 6) {
            	if (flag.equals("boyrank") && booklist_boyrank.size() >= 6)
                {
                    loading.setVisibility(8);
                    area_boy.setVisibility(0);
                    setImage(booklist_boyrank, 0x7f060053);
                    boy_item1_title.setText(((Book)booklist_boyrank.get(0)).getName());
                    boy_item1_author.setText(((Book)booklist_boyrank.get(0)).getAuthor());
                    boy_item1_introduce.setText(((Book)booklist_boyrank.get(0)).getIntroduction());
                    boy_item2_title.setText(((Book)booklist_boyrank.get(1)).getName());
                    boy_item2_introduce.setText(((Book)booklist_boyrank.get(1)).getIntroduction());
                    boy_item3_title.setText(((Book)booklist_boyrank.get(2)).getName());
                    boy_item3_introduce.setText(((Book)booklist_boyrank.get(2)).getIntroduction());
                    boy_item4_title.setText(((Book)booklist_boyrank.get(3)).getName());
                    boy_item4_introduce.setText(((Book)booklist_boyrank.get(3)).getIntroduction());
                    boy_item5_title.setText(((Book)booklist_boyrank.get(4)).getName());
                    boy_item5_introduce.setText(((Book)booklist_boyrank.get(4)).getIntroduction());
                    boy_item6_title.setText(((Book)booklist_boyrank.get(5)).getName());
                    boy_item6_introduce.setText(((Book)booklist_boyrank.get(5)).getIntroduction());
                } else
                if (flag.equals("girlrank") && booklist_girlrank.size() >= 6)
                {
                    loading.setVisibility(8);
                    area_girl.setVisibility(0);
                    setImage(booklist_girlrank, 0x7f06006a);
                    girl_item1_title.setText(((Book)booklist_girlrank.get(0)).getName());
                    girl_item1_author.setText(((Book)booklist_girlrank.get(0)).getAuthor());
                    girl_item1_introduce.setText(((Book)booklist_girlrank.get(0)).getIntroduction());
                    girl_item2_title.setText(((Book)booklist_girlrank.get(1)).getName());
                    girl_item2_introduce.setText(((Book)booklist_girlrank.get(1)).getIntroduction());
                    girl_item3_title.setText(((Book)booklist_girlrank.get(2)).getName());
                    girl_item3_introduce.setText(((Book)booklist_girlrank.get(2)).getIntroduction());
                    girl_item4_title.setText(((Book)booklist_girlrank.get(3)).getName());
                    girl_item4_introduce.setText(((Book)booklist_girlrank.get(3)).getIntroduction());
                    girl_item5_title.setText(((Book)booklist_girlrank.get(4)).getName());
                    girl_item5_introduce.setText(((Book)booklist_girlrank.get(4)).getIntroduction());
                    girl_item6_title.setText(((Book)booklist_girlrank.get(5)).getName());
                    girl_item6_introduce.setText(((Book)booklist_girlrank.get(5)).getIntroduction());
                }
            } else {
            	loading.setVisibility(8);
                area_total.setVisibility(0);
                setImage(booklist_totalrank, 0x7f06003b);
                total_item1_title.setText(((Book)booklist_totalrank.get(0)).getName());
                total_item1_author.setText(((Book)booklist_totalrank.get(0)).getAuthor());
                total_item1_introduce.setText(((Book)booklist_totalrank.get(0)).getIntroduction());
                total_item2_title.setText(((Book)booklist_totalrank.get(1)).getName());
                total_item2_introduce.setText(((Book)booklist_totalrank.get(1)).getIntroduction());
                total_item3_title.setText(((Book)booklist_totalrank.get(2)).getName());
                total_item3_introduce.setText(((Book)booklist_totalrank.get(2)).getIntroduction());
                total_item4_title.setText(((Book)booklist_totalrank.get(3)).getName());
                total_item4_introduce.setText(((Book)booklist_totalrank.get(3)).getIntroduction());
                total_item5_title.setText(((Book)booklist_totalrank.get(4)).getName());
                total_item5_introduce.setText(((Book)booklist_totalrank.get(4)).getIntroduction());
                total_item6_title.setText(((Book)booklist_totalrank.get(5)).getName());
                total_item6_introduce.setText(((Book)booklist_totalrank.get(5)).getIntroduction());
            }
        }

        public void setFlag(String s)
        {
            flag = s;
        }
    }

	private class Myrunnable2image implements Runnable {
		private Bitmap bitmap = null;
		private ImageView coverImageView = null;

		public Myrunnable2image(ImageView paramImageView) {
			this.coverImageView = paramImageView;
		}

		public void run() {
			this.coverImageView.setImageBitmap(this.bitmap);
		}

		public void setBitmap(Bitmap paramBitmap) {
			this.bitmap = paramBitmap;
		}
	}
}